<?php
/**
 *
 * 功能描述（订单数据）
 *
 * @author suoya <jlusuoya@gmail.com>
 * @version 1
 */

class ModelOrder extends ModelBase
{
	function GetList($page, $where, $type, $c_start, $c_end, $d_start, $d_end, $key, $keyword)
	{
		if($type != 0)
			$where .= " and type= $type";
		if($c_start != '')
			$where .= " and createtime >= '$c_start'";
		if($c_end != '')
			$where .= " and createtime <= '$c_end'";
		if($d_start != '')
			$where .= " and delivery_time >= '$d_start'";
		if($d_end != '')
			$where .= " and delivery_time <= '$d_end'";
		if(!empty($key) && !empty($keyword))
			$where .= " and $key like '%$keyword%'";
		$data  = $this->db->getRow("order_base", 'count(1) cnt', $where);
		if($data['cnt'] == 0)
			return array('cnt' => 0, 'pageCnt' => 0, 'data' => array());

		$retData = array('cnt' => $data['cnt'], 'pageCnt' => ceil($data['cnt'] / MSG_PAGESIZE));
		$limit = MSG_PAGESIZE * ($page - 1) . ", " . MSG_PAGESIZE;
		$fild = "`id`, `type`, `userid`, `mobile_from`, `consignee`, `mobile_to`, `delivery_time`, `createtime`, `add_adm`, `cofm_adm`, `pay_adm`, `status`";
		$data = $this->db->getAll("order_base", $fild, $where, 'id desc', $limit);
		$retData['data'] = $data;
		return $retData;
	}

	function UpdateOrder($data, $id, $where = '')
	{
		$cnt = $this->db->Update("order_base", $data, "id = $id $where");
		return $cnt;
	}

	function GetBaseInfo($id)
	{
		$fild = "`id`, `status`, `pay_type`, `pay_adm`, `delivery_type` dely_type, `delivery_time` dely_time, `mobile_from`, `mobile_to`, `message`, `amount`, `userid`, `add_score`";
		return $data = $this->db->GetRow("order_base", $fild, "id = $id");
	}

	function GetAllInfo($id)
	{
		$fild1 = 't1.`id`, `type`, `userid`, t1.`status`, `add_adm`, `cofm_adm`, `createtime`, `pay_type`, `pay_adm`, `trade_money1`, `trade_money2`, `money1_code`, `money2_code`, `add_score`, `amount_pre`, `amount`, `card1`, `card2`, `attach1`, `attach2`, `area`, `address`, `consignee`, `mobile_to`, `mobile_from`, `sign_building`, `zipcode`, `delivery_type`, `delivery_time`, `inv_type`, `inv_title`, `message`';
		$fild2 = 't2.`id` delyid, t2.`status` delystatus, `dely_adm`, `starttime`, `finishtime`, `note`';
		$sql = "select $fild1, $fild2 from `order_base` t1 left join `dely_info` t2 on t1.id = t2.id where t1.id = $id";
		$data = $this->db->getRowBySql($sql, 'Row');
		return $data;
	}

	function GetAllGoods($id)
	{
		$fild1 = 't1.`id`, `order_id`, `good_id`, `name`, `version`, `size`, `price`, `cnt`, `discount`, `comment_id`';
		$fild2 = 't2.`id` prod_id, `item_id`, `status`, `prod_adm`, `timelimit`, `finishtime`, `taketime`, `takeman`, `note`';
		$sql = "select $fild1, $fild2 FROM `prod_info` t2 right join `order_goods` t1 on t1.id = t2.item_id where order_id = $id order by order_id desc";
		$data = $this->db->getRowBySql($sql, 'All');
		return $data;
	}

	function Create($data)
	{
		$id = $this->db->Insert("order_base", $data);
		return $id;
	}

	function SaveData($id, $cart)
	{
		$sql = "insert into order_goods(`order_id`, `good_id`, `name`, `version`, `size`, `price`, `cnt`, `discount`) values";
		foreach($cart as $item)
		{
			if($item['status'] == 0)
				$sql .= "($id, " . $item['id'] . ", '" . $item['name'] . "', " . $item['version']
					. ", " . $item['size'] . ", " .$item['price'] . ", " . $item['cnt'] . ", " . $item['discount'] ."),";
		}
		$sql = substr($sql, 0, strlen($sql)-1);
		$id = $this->db->execute($sql);
		return $id;
	}

	function NoticeProd($id)
	{
		$data = $this->db->GetRow("order_base", "message, delivery_time", "id = $id");
		if(!$data)
			return 0;
		$sql = "insert into prod_info(item_id, timelimit, note) select id, '" . $data['delivery_time']."', '".$data['message']."' from order_goods where order_id = $id";
		$cnt = $this->db->execute($sql);
		return $cnt;
	}

	function NoticeDely($id)
	{
		$sql = "insert into dely_info(`id`) select id from order_base where id = $id and delivery_type = 1";
		$id = $this->db->execute($sql);
		return $id;
	}
}

